package com.qidian.study.servlets;

import com.qidian.study.pojo.ContentCat;
import com.qidian.study.service.ContentCatService;
import com.qidian.study.service.impl.ContentCatServiceImpl;
import com.qidian.study.util.DateFormat;
import com.qidian.study.util.JsonTools;
import com.qidian.study.util.pojo.VideoResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
@WebServlet("/contentCat")
public class ContentCatServlet extends BaseServlet {
    private ContentCatService catService = new ContentCatServiceImpl();
    public void add(HttpServletRequest request, HttpServletResponse response)
        throws IOException,ServletException{
        String catName = request.getParameter("catName");
        String catDesc = request.getParameter("catDesc");
        ContentCat cat = new ContentCat();
        cat.setCatDesc(catDesc);
        cat.setCatName(catName);
        cat.setStatus(1);
        cat.setCreateTime(DateFormat.getNowString());
        cat.setModifyTime(DateFormat.getNowString());
        // 调用servic的save方法。
        VideoResponse result = catService.save(cat);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(result));
    }
    public void queryById(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        long catId = new Long(request.getParameter("catId"));
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(catService.queryById(catId)));
    }
    public void update(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        long catId = new Long(request.getParameter("catId"));
        String catName = request.getParameter("catName");
        String catDesc = request.getParameter("catDesc");
        ContentCat cat = new ContentCat();
        cat.setCatId(catId);
        cat.setCatDesc(catDesc);
        cat.setCatName(catName);
        cat.setStatus(1);
        cat.setModifyTime(DateFormat.getNowString());
        VideoResponse result = catService.update(cat);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(result));
    }
    public void queryAll(HttpServletRequest request, HttpServletResponse response)
            throws IOException,ServletException{
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(catService.queryAllForLayUi()));
    }
}
